﻿#include "stdafx.h"
#include "AsioSignalTest.h"

#include <iostream>

// asio 信号测试程序

#define BOOST_ASIO_ENABLE_HANDLER_TRACKING		// 启用跟踪日志
#include <boost/asio.hpp>

void AsioSignalTest()
{
	using namespace boost::asio;
	using namespace boost::system;

	boost::asio::io_service io;
	signal_set sig(io, SIGINT, SIGILL);
	//sig.add(SIGINT);
	//sig.add(SIGUSR1);

	auto handler1 = [](const error_code& ec, int n)
	{
		if (ec)
		{
			std::cout << ec.message() << std::endl;
			return;
		}
		if (n != SIGINT)
		{
			return;
		}
		std::cout << "handler1 recv = " << n << std::endl;
		std::cout << "do something" << std::endl;
	};

	typedef void (handler_type)(const error_code&, int);

	std::function<handler_type> handler2 = [](const error_code& ec, int n)
	{
		if (n != SIGILL)
		{
			return;
		}
		std::cout << "handler2 recv = " << n << std::endl;
	};

	sig.async_wait(handler1);
	sig.async_wait(handler2);

	io.run();
	std::cout << "io stoped" << std::endl;

}